<?php
/**
 * Created by PhpStorm.
 * User: FDG367
 * Date: 2017/9/27
 * Time: 16:01
 */
namespace common\widgets;

use Yii;
use yii\bootstrap\Html;
use \yii\bootstrap\Widget;
use yii\helpers\ArrayHelper;
use yii\web\JsExpression;

class Calender extends Widget{
	public $year;
	public $month;
	public $day;



	public function init(){
		parent::init();
	}

	public function run()
	{
		//加载数据
		$year	= (1970!=$this->year) ?$this->year:date('Y');
		$month	= $this->month?$this->month:1;
		$day	= $this->day?$this->day:1;

		//js
		$jsScript = <<<EOF

		$('document').ready(function(){
		var year = "{$year}";
		var calender_month = "{$month}";
		var calender_day = "{$day}";
		//mod by FDG367 【商城需求】个人中心/出生日期默认时间修改 448 start
		//var i=1900;
		var i=1910;
		//mod by FDG367 【商城需求】个人中心/出生日期默认时间修改 448 end
    var date = new Date();
   // year = date.getFullYear();//获取当前年份
    var dropList;
    for(i;i<2040;i++){
        if(i == year){
            dropList = dropList + "<option value='"+i+"' selected>"+i+"</option>";
        }else{
            dropList = dropList + "<option value='"+i+"'>"+i+"</option>";
        }
    }
    $('select[name=year]').html(dropList);//生成年份下拉菜单
    var monthly;
    for(month=1;month<13;month++){
        monthly = monthly + "<option value='"+month+"'>"+month+"</option>";
    }
    $('select[name=month]').html(monthly);//生成月份下拉菜单
    var dayly;
    for(day=1;day<=31;day++){
        dayly = dayly + "<option value='"+day+"'>"+day+"</option>";
    }
    $('select[name=day]').html(dayly);//生成天数下拉菜单
    /*
     * 处理每个月有多少天---联动
     */
    $('select[name=month]').change(function(){
        var currentDay;
        var Flag = $('select[name=year]').val();
        var currentMonth = $('select[name=month]').val();
        switch(currentMonth){
            case "1" :
            case "3" :
            case "5" :
            case "7" :
            case "8" :
            case "10" :
            case "12" :total = 31;break;
            case "4" :
            case "6" :
            case "9" :
            case "11" :total = 30;break;
            case "2" :
                if((Flag%4 == 0 && Flag%100 != 0) || Flag%400 == 0){
                    total = 29;
                }else{
                    total = 28;
                }
            default:break;
        }
        for(day=1;day <= total;day++){
            currentDay = currentDay + "<option value='"+day+"'>"+day+"</option>";
        }
        $('select[name=day]').html(currentDay);//生成日期下拉菜单
    })

	$('select[name=year]').val(year);
	$('select[name=month]').val(calender_month);
	$('select[name=day]').val(calender_day);
});
EOF;

		//处理数据
		$view = $this->getView();
		$view->registerJs((new JsExpression($jsScript)));
		return  '<div class="item item_ext">年<select name="year"></select></div><div class="item item_ext">月<select name="month"></select></div><div class="item item_ext">日<select name="day"></select></div>';
	}

}